package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.PatrolRouteManagement;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.OrganizationalStructureService;
import com.yq.cp.service.basics.PatrolRouteManagementService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 巡逻线路管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/patrolRouteManagement")
public class PatrolRouteManagementController {
    @Resource
    PatrolRouteManagementService patrolRouteManagementService;
    @Resource
    OrganizationalStructureService organizationalStructureService;
    /**
     * 添加编辑-子类先全删在添加
     * @param patrolRouteManagement
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody PatrolRouteManagement patrolRouteManagement){
        patrolRouteManagementService.add(patrolRouteManagement);
        return ApiResult.success();
    }

    /**
     * 删除
     * @param patrolRouteManagement
     * @return
     */
    @PostMapping("delete")
    public ApiResult delete(@RequestBody PatrolRouteManagement patrolRouteManagement){
        patrolRouteManagementService.delete(patrolRouteManagement);
        return ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        Page<PatrolRouteManagement> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<PatrolRouteManagement> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("name",vo.getLike());
        wrapper.eq("site_id",vo.getSiteId());
        wrapper.orderByDesc("create_time");
        page = patrolRouteManagementService.page(page,wrapper);
        if (!page.getRecords().isEmpty()){
            for (PatrolRouteManagement record : page.getRecords()) {
                OrganizationalStructure byId = organizationalStructureService.getById(record.getSiteId());
                record.setSiteName(byId.getName());
            }
        }
        return ApiResult.success(page);
    }

    /**
     * 详情
     * @param vo
     * @return
     */
    @PostMapping("detail")
    public ApiResult detail(@RequestBody PatrolRouteManagement vo){
        PatrolRouteManagement patrolRouteManagement = patrolRouteManagementService.detail(vo);
        return ApiResult.success(patrolRouteManagement);
    }
}

